package com.glory.gloryquickui.view

import android.content.Context
import android.content.Intent
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.glory.gloryquickui.R
import com.glory.gloryquickui.activity.DemoActivity
import com.glory.gloryquickui.entity.FieldBean
import com.glory.gloryquickui.utils.ScreenUtil
import com.glory.gloryquickui.viewmodel.QuickUIViewModel
import com.glory.gloryquickui.widget.edit.ClearEditText


/**
 *Created:YJ
 *创建时间：2021/5/13
 *Describe:
 */
class RowViewController(context: Context?, val viewModel: QuickUIViewModel) :
    ViewController<FieldBean?>(context) {

    private var rowLayout: LinearLayout? = null

    override fun resLayoutId(): Int {
        return R.layout.layout_row_view
    }

    override fun onCreatedView(view: View?) {
        rowLayout = view?.findViewById(R.id.rowLayout)
    }

    override fun onBindView(data: FieldBean?) {
        createLayout()
    }

    fun createLayout() {
        data?.config?.children?.forEach {
            if (it.config.tagIcon.equals("button")) {
                val buttonlayout: View =
                    LayoutInflater.from(context).inflate(R.layout.layout_button, null)
                val btnMain: Button = buttonlayout?.findViewById(R.id.btnMain)
                btnMain.text = it.slot.default
                val params = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT)
                params.weight = it.config.span.toFloat()
                buttonlayout.layoutParams = params
                rowLayout?.addView(buttonlayout)
                btnMain.setOnClickListener { view ->
                    setEvent(it)
                }
            } else if (it.config.tagIcon.equals("input")) {
                val map = viewModel.pageData.value!!.toMutableMap()
                if (it.config.defaultValue is String) {
                    map[it.vModel] = it.config.defaultValue.toString()
                } else {
                    if (it.vModel.isNotEmpty()){
                        map[it.vModel] = ""
                    }
                }
                val edittextlayout: View =
                    LayoutInflater.from(context).inflate(R.layout.layout_single_edittext, null)
                val tvSingleEditName: TextView = edittextlayout.findViewById(R.id.tvSingleEditName)
                val tvSingleEditValue: ClearEditText =
                    edittextlayout?.findViewById(R.id.tvSingleEditValue)
                tvSingleEditName!!.text = it?.config?.label
                tvSingleEditValue!!.hint = data?.placeholder
                if (it!!.config.defaultValue != null && it.config.defaultValue is String) {
                    tvSingleEditValue!!.setText(it.config.defaultValue.toString())
                }
                //设置可禁止
                tvSingleEditValue!!.isEnabled = !it.disabled
                if (it.readonly) {
                    tvSingleEditValue!!.inputType = InputType.TYPE_NULL
                }
                val params = LinearLayout.LayoutParams(0, ScreenUtil.dip2px(context!!, 48F))
                params.width = 0
                params.weight = it.config.span.toFloat()
                params.height = ScreenUtil.dip2px(context!!, 48F)
                edittextlayout.layoutParams = params
                rowLayout?.addView(edittextlayout)
                tvSingleEditValue?.addTextChangedListener(object : TextWatcher {
                    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

                    }

                    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                        if (!it.readonly) {
                            val map = viewModel.pageData.value!!.toMutableMap()
                            map[it.vModel] = p0.toString()
                            viewModel.pageData.value = map
                            if (p1 == 0 && p2 == 0 && p3 > 1) {
                                if (p0.toString().length > 0 && it.slot.append.equals("1")) {
                                    viewModel.searchData()
                                }
                            }
                        }
                    }

                    override fun afterTextChanged(p0: Editable?) {

                    }
                })
            }
        }
    }

    private fun setEvent(data: FieldBean) {
        when (data.config.eventType) {
            "manual" -> {
                if (data.config.eventName == "saveForm") {
                    viewModel.saveData()
                } else if (data.config.eventName == "clearForm") {
                    viewModel.searchData()
                } else if (data.config.eventName == "backPage") {
                } else if (data.config.eventName.contains("get")) {
                    viewModel.searchData()
                } else if (data.config.eventName.contains("delete")) {

                }
            }
            "interface" -> {

            }
            "froms" -> {
                val intent = Intent(context, DemoActivity::class.java)
                intent.putExtra("json", data.config.fromId)
                context!!.startActivity(intent)
            }
            "pageInteraction" -> {

            }
        }
    }
}